package com.qf.action;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.qf.base.BaseServlet;
import com.qf.model.User;
import com.qf.service.impl.UserServiceImpl;
import com.qf.utils.CommonsUtils;
import com.qf.utils.MD5Utils;
import com.qf.utils.SendJMail;

/**
 * Servlet implementation class UserServlet
 */
public class UserServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
    
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	
	protected void loginOut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html:charset=utf-8");
		
		request.getSession().invalidate();
		Cookie c = new Cookie("uname","");
		Cookie c1 = new Cookie("upwd","");
		c.setMaxAge(0);
		c1.setMaxAge(0);
		response.addCookie(c);
		response.addCookie(c1);
		response.sendRedirect("index.jsp");
	}

	
	protected void findByName(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		
		String uname = request.getParameter("uname");
		boolean b=userServiceImpl.findByName(uname);
		if(b){
			response.getWriter().write("{\"flag\":\"1\"}");
		}else{
			response.getWriter().write("{\"flag\":\"0\"}");

		}
		
		
		
	}
	
	protected void codeServlet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		String code = request.getParameter("code");
		String code1 = (String) request.getSession().getAttribute("checkcode_session");
		
		if(code.equals(code1)){
			response.getWriter().write("{\"flag\":\"0\"}");
		}else{
			response.getWriter().write("{\"flag\":\"1\"}");

		}
		
		
		
	}
	
	protected void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;utf-8");
		
		Map<String, String[]> map = request.getParameterMap();
		User u = new User();
		try {
			BeanUtils.populate(u, map);
		} catch (IllegalAccessException e) {
			
			//TODO
			e.printStackTrace();
				
		} catch (InvocationTargetException e) {
			
			//TODO
			e.printStackTrace();
				
		}
		u.setUid(CommonsUtils.getUUID());
		u.setCode(CommonsUtils.getUUID());
		u.setUpwd(MD5Utils.md5(request.getParameter("upwd")));
		boolean b = userServiceImpl.add(u);
		if(b){
			//发送激活邮件
			String emailMsg="http://localhost:8080/"+request.getSession().getServletContext().getContextPath()+"/userServlet?uid="+u.getUid()+"&code="+u.getCode()+"&action=activeCode";
			SendJMail.sendMail(u.getUemail(), emailMsg);
			response.sendRedirect("registersuccess.jsp");
			
		}else{
			request.setAttribute("error", "服务错误,注册失败");
			response.sendRedirect("register.jsp");
		}
		
		
	}
	
	
	protected void activeCode(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
        String uid = request.getParameter("uid");
        String code = request.getParameter("code");
        
       boolean b= userServiceImpl.findother(uid,code);
       if(b){
    	   boolean b1=userServiceImpl.update(uid);
    	   if(b1){
    		   request.getRequestDispatcher("activesuccess.jsp").forward(request, response);
    	   }else{
    		   request.getRequestDispatcher("registersuccess.jsp").forward(request, response);
    	   }
       }
		
	}
	
	protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String uname = request.getParameter("uname");
		String upwd = request.getParameter("upwd");
		String autoLogin = request.getParameter("autoLogin");
		String check = request.getParameter("rememberMe");
		Cookie c = new Cookie("uname", uname);
		Cookie c1 = new Cookie("upwd",upwd);
		Cookie c2 = new Cookie("autoLogin",autoLogin);
		User u=userServiceImpl.find(uname, upwd);
		boolean b=userServiceImpl.findNameandPwd(uname,upwd);
		if(b){
			boolean b1=userServiceImpl.findBystate(uname);
			if(b1){
				if("on".equals(check)){
					c.setMaxAge(24*3600);
					c1.setMaxAge(24*3600);
				}else{
					c.setMaxAge(0);
					c1.setMaxAge(0);
					
				}
				response.addCookie(c);
				response.addCookie(c1);
				if("on".equals(autoLogin)){
					c2.setMaxAge(24*3600);
				}
				response.addCookie(c2);
				request.getSession().setAttribute("user", u);
				request.getRequestDispatcher("index.jsp").forward(request, response);
			}else{
				request.setAttribute("error", "该账号未激活");
				request.getRequestDispatcher("login.jsp").forward(request, response);
			}
		}else{
			request.setAttribute("error", "密码错误");
			request.getRequestDispatcher("login.jsp").forward(request, response);
			
		}
		
	}
	

}
